import { useEffect,useState } from "react"
export const isFalsy = (value: unknown) => value === 0 ? false : !value

export const isVoid = (value:unknown) => value === undefined || value === null || value === ''

// let a: object
// a = {name:'jack'}
// 在一个函数里，改变传入的对象本身是不好的
export const cleanObject = (object: {[key:string]:unknown}) => {
  const result = { ...object }
  Object.keys(result).forEach(key => {
    const value = result[key]
    if (isVoid(value)) {
      delete result[key]
    }
  })
  return result
}
export const useMount = (callback: () => void) => {
  useEffect(() => {
    callback()
    // TODO 依赖项里加上callback会造成无限循环，这个和  useCallback和useMeno有关系
  }, []);
}

// 后面用泛型来规范类型
export const useDebounce = <V>(value: V, delay?: number) => {
  const [debouncedValue, setDebouncedValue] = useState(value)

  useEffect(() => {
    // 每次在value delay变化以后设置一个定时器
    const timeout = setTimeout(() => setDebouncedValue(value), delay)
    // 每次在上一个useEffect处理完后再运行
    return () => clearTimeout(timeout)
  }, [value, delay])

  return debouncedValue
}

// 学习路线  项目经历  职业规划